<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>14-开关灯案例</title>
</head>

<body>

  <button>开关灯</button>
  <script>
    var flag = 0;
    var btn = document.querySelector('button');
    var body = document.body;

    btn.onclick = function () {
      flag = !flag;
      if (flag) {
        body.style.backgroundColor = 'black';
      } else {
        body.style.backgroundColor = 'white';
      }
    }
  </script>

</body>

</html>